﻿using UnityEngine;
using System.Collections;

public class NeuronQuadratic : Neuron {
	
	/*public long synapse;
	public ushort soma;
	public ushort axon;
*/

	public float f02 = 0.0f;
	public float f12 = 0.5f;
	public float f22 = 1.0f;

	public float a, b, c;


	public float probeInputFloat = 0.5f;
	public float probeOutputFloat = 0.5f;
	
	void Start () {
		axon = soma = 32767;
	}

	public ushort activationFunction(ushort u) {
		float f = Mathf.Clamp01 (u / 65534.0f);

		c = f02;
		a = f22 + c - f12 * 2;
		b = f22 - a - c;
		f = a*f*f+b*f+c;

		//f = f12 + (f - 0.5f) * (f22 - f02);

		f = Mathf.Clamp01 (f);
		return (ushort)(f * 65534.0f);
	}
	
	void FixedUpdate () {
		if (synapse > 65534)
			synapse = 65534;
		if (synapse < 0)
			synapse = 0;
		soma = (ushort)synapse;
		synapse = 32767;
		
		axon = activationFunction(soma);
	}
	/*
	
	void FixedUpdate () {
		if (synapse > 65534)
			synapse = 65534;
		if (synapse < 0)
			synapse = 0;
		soma = (ushort)synapse;
		synapse = 32767;

		if (false) {
			axon = soma;
		} else if (false) {
			probeInputFloat = Mathf.Clamp01 (soma / 65534.0f);
			float f = f12;
			probeOutputFloat = Mathf.Clamp01 (f);
			axon = (ushort)(f * 65534.0f);
		} else {
			probeInputFloat = Mathf.Clamp01 (soma / 65534.0f);
			float f = probeInputFloat;
			
			c = f02;
			a = f22 + c - f12 * 2;
			b = f22 - a - c;
			f = a*f*f+b*f+c;

			probeOutputFloat = Mathf.Clamp01 (f);
			axon = (ushort)(probeOutputFloat * 65534.0f);
	}
	
	
	
*/


		/*
		float a, b, c;
		c = f02;
		a = f22 + c - f12 * 2;
		b = f22 - a - c;



		a/4+b/2+c=f12
		a+b+c=f22
		
		b = f22-c-a;
		a/4+f22/2-c/2-a/2+c=f12
		a/4-a/2=-f22/2+c/2-c+f12
		a/2=f22/2+c/2-f12
		a=f22+c-f12*2

*/

	
	public override float activationFunctionf(float f) {
		return activationFunction ((ushort)(f * 65534.0f)) / 65534.0f;
	}





}
